//
//  CommentListView.m
//  zhijianshejian
//
//  Created by 朱传正 on 16/9/10.
//  Copyright © 2016年 haoniu-duhuan. All rights reserved.
//

#import "CommentListView.h"
#import "NSString+Size.h"
#import "NSString+Date.h"
#import "CommentListModel.h"

#define gap 8

@interface CommetCellView : UIView

@property(nonatomic,strong)CommentListModel *model;

-(id)initWithModel:(CommentListModel *)model andRect:(CGRect)rect;

/*评论头像*/
@property (strong, nonatomic) UIImageView *commentImageView;
/*评论人*/
@property (strong, nonatomic) UILabel *commentNameLabel;
/*评论内容*/
@property (strong, nonatomic) UILabel *commentContentLabel;
/*评论时间*/
@property (strong, nonatomic) UILabel *commentTimeLabel;

@property (nonatomic)CGRect cellRect;

@end

@implementation CommetCellView

-(UIImageView *)commentImageView{
    if (!_commentImageView) {
        _commentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(gap, gap, 30, 30)];
        _commentImageView.layer.masksToBounds = YES;
        _commentImageView.layer.cornerRadius = _commentImageView.frame.size.height/2;
        [_commentImageView sd_setImageWithURL:_model.userImg placeholderImage:[UIImage imageNamed:@"detailImage.png"]];
    }
    return _commentImageView;
}

-(UILabel *)commentNameLabel{
    if (!_commentNameLabel) {
        CGFloat originX = _commentImageView.frame.origin.x + _commentImageView.frame.size.width + gap;
        CGFloat originY = _commentImageView.frame.origin.y + 3 ;
        _commentNameLabel = [[UILabel alloc]initWithFrame:CGRectMake( originX, originY, kScreen_Width - originX - gap, 15)];

        _commentNameLabel.textColor = RGBJ(40);
        _commentNameLabel.numberOfLines = 1;
        _commentNameLabel.font = [UIFont systemFontOfSize:13];
        _commentNameLabel.text = _model.username;
    }
    return _commentNameLabel;
}

-(UILabel *)commentTimeLabel{
    if (!_commentTimeLabel) {
        CGFloat originX = _commentNameLabel.frame.origin.x;
        CGFloat originY = _commentNameLabel.frame.origin.y + _commentNameLabel.frame.size.height;
        _commentTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(originX, originY, kScreen_Width - originX - gap, 14)];
        _commentTimeLabel.textColor = RGBJ(155);
        _commentTimeLabel.numberOfLines = 1;
        _commentTimeLabel.font = [UIFont systemFontOfSize:11];
        
        
        _commentTimeLabel.text = [NSString stringWithFormat:@"%@",_model.replyTime];
    }
    return _commentTimeLabel;
}

-(UILabel *)commentContentLabel{
    if (!_commentContentLabel) {
        CGFloat originX = gap;
        CGFloat originY = _commentImageView.frame.origin.y + _commentImageView.frame.size.height + gap;
        _commentContentLabel = [[UILabel alloc]initWithFrame:CGRectMake(originX, originY, kScreen_Width - 2*gap, 20)];
        _commentContentLabel.textColor = RGB(2, 2, 2);
        _commentContentLabel.numberOfLines = 0;
        _commentContentLabel.font = [UIFont systemFontOfSize:14];
        CGFloat contentHeight = [_model.content heightWithFont:[UIFont systemFontOfSize:14.0f] constrainedToWidth:((_commentContentLabel.frame.size.width ))];
        CGRect frame = _commentContentLabel.frame;
        frame.size.height = contentHeight;
        _commentContentLabel.frame = frame;
        self.commentContentLabel.text = _model.content;
    }
    return _commentContentLabel;
}



-(id)initWithModel:(CommentListModel *)model andRect:(CGRect)rect{
    if (self = [super init]) {
        self.cellRect = rect;

        self.frame = CGRectMake(0, rect.size.height, rect.size.width, 0);
        self.model = model;
        UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, 0.5)];
        lineLabel.backgroundColor = RGBJ(225);;
        [self addSubview:lineLabel];
        [self addSubview:self.commentImageView];
        [self addSubview:self.commentNameLabel];
        [self addSubview:self.commentContentLabel];
        [self addSubview:self.commentTimeLabel];
        CGRect frame = self.frame;
        frame.size.height = _commentContentLabel.frame.size.height + 60;
        self.frame = frame;
    }
    return self;
}

@end

@interface CommentListView ()

@property (nonatomic,assign) NSInteger dataCount;

@end

@implementation CommentListView

-(NSInteger)dataCount{
    if (!_dataCount) {
        _dataCount = 0;
    }
    return _dataCount;
}

-(id)initWithCGRect:(CGRect)rect withData:(NSArray *)data{
    if(self = [super initWithFrame:rect]){
        self.frame = CGRectMake(0, rect.size.height, rect.size.width, 0);
        
        for (NSInteger i = _dataCount; i<data.count; i++) {
            CommentListModel *model = [[CommentListModel alloc]init];
            
            model.username = [data[i] objectForKey:@"nickname"];
            
            
            model.replyTime = [data[i] objectForKey:@"createTime"];
//            model.replyTime = [self timeString:[NSString stringWithFormat:@"%@",[data[i] objectForKey:@"replyTime"]]];
            
            model.content = [data[i] objectForKey:@"content"];
            
            model.userImg = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",url_Base,[data[i] objectForKey:@"photo"]]];
            
            CommetCellView *commentCellView = [[CommetCellView alloc]initWithModel:model andRect:self.frame];
            [self addSubview:commentCellView];
            CGRect frame = self.frame;
            frame.size.height = frame.size.height + commentCellView.frame.size.height;
            self.frame = frame;
        }
        _dataCount = data.count;
    }
    return self;
}


//连在一起的数字字符，转化为时间
-(NSString *)timeString:(NSString *)timestr{
    //NSString *str=@"1368082020";//时间戳
    NSTimeInterval time=[timestr doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}


@end


